//
//  GYBottomTool.m
//  GoodLuck
//
//  Created by apple on 2018/2/5.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYBottomTool.h"
#import "GYButton.h"
#import "UIButton+GYExtension.h"
#import "GYNetWorkTools.h"

@interface GYBottomTool()

/** 购物车商品数量的标记 */
@property (nonatomic, strong) UILabel *shopingCartGoodsNumLable;
@end

@implementation GYBottomTool

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        //监听通知
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(updateShopingCartGoodsNum) name:updateShopingCartGoodsNum object:nil];
        
        //初始化子控件
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI {
    
    //添加子控件
    [self creatGYButton:CGRectMake(0, 0, ScreenWidth/6-5, self.height) title:@"客服" image:@"kefu_b" tag:1];
    [self creatGYButton:CGRectMake(ScreenWidth/6-5, 0, ScreenWidth/6+10, self.height) title:@"购物车" image:@"cart_b" tag:2];
    [self creatGYButton:CGRectMake(2*ScreenWidth/6+5, 0, ScreenWidth/6-5, self.height) title:@"店铺" image:@"store_b" tag:3];
    
    [self creatButton:CGRectMake(ScreenWidth/2 + ScreenWidth/4+15, 0, ScreenWidth/4-15, self.height) title:@"立即购买" backgrounColor:RGBColor(255, 138, 1) tag:4];
    [self creatButton:CGRectMake(ScreenWidth/2 , 0, ScreenWidth/4+15, self.height) title:@"加入购物车" backgrounColor:RGBColor(0, 158, 214) tag:5];//RGBColor(255, 138, 1)
    
    //添加购物车上面的数量标记
    self.shopingCartGoodsNumLable = [[UILabel alloc] init];
    self.shopingCartGoodsNumLable.translatesAutoresizingMaskIntoConstraints = false;
    self.shopingCartGoodsNumLable.textColor = [UIColor redColor];
    self.shopingCartGoodsNumLable.font = [UIFont systemFontOfSize:14];
    self.shopingCartGoodsNumLable.layer.borderWidth = 1;
    self.shopingCartGoodsNumLable.layer.borderColor = [UIColor redColor].CGColor;
    self.shopingCartGoodsNumLable.layer.cornerRadius = 9;
    self.shopingCartGoodsNumLable.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.shopingCartGoodsNumLable];
    
    //布局控件
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.shopingCartGoodsNumLable attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:-ScreenWidth/3 *2]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.shopingCartGoodsNumLable attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
    [self addConstraint:[NSLayoutConstraint constraintWithItem:self.shopingCartGoodsNumLable attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:17]];
    
    //初始化的时候应该要请求购物车商品数量
    [self updateShopingCartGoodsNum];
}

- (void)btuClick:(GYButton *)btu {
    [self.delegate onButtonClick:btu.tag];
}

//立即购买和购物车
- (void)onClick:(UIButton *)btu {
    [self.delegate onButtonClick:btu.tag];
}

//通知更新购物车商品的数量
- (void)updateShopingCartGoodsNum {
    [GYNetWorkTools requestQueryShopingCartGoodsNum:^(NSDictionary *obj) {
        //NSLog(@"购物车obj=======%@",obj);
        int code = [[obj objectForKey:@"code"] intValue];
        NSDictionary *datas = [obj objectForKey:@"datas"];
        if (code == 200) {
            NSString *cart_count = [NSString stringWithFormat:@"%@",[datas objectForKey:@"cart_count"]];
            if ([cart_count intValue] == 0) {
                self.shopingCartGoodsNumLable.hidden = YES;
            }else {
                self.shopingCartGoodsNumLable.hidden = NO;
            }
            self.shopingCartGoodsNumLable.text = cart_count;
        }
    } error:^(NSError *error) {
        NSLog(@"QueryShopingCartGoodsNumFailed====%@",error);
    }];
}

/**
 创建按钮

 @param title <#title description#>
 @param imageName <#imageName description#>
 */
- (void)creatGYButton:(CGRect)frame title:(NSString *)title image:(NSString *)imageName tag:(NSInteger)tag {
    
    GYButton *chatBtu = [GYButton buttonWithType:GYButtonTypeVerticalImageTitle];
    chatBtu.frame = frame;
    chatBtu.fontSize = widthScale(14, iphone6_W);
    chatBtu.tag = tag;
    [chatBtu setImage:[UIImage imageNamed:imageName] forState:GYControlStateNormal];
    [chatBtu setTitle:title forState:GYControlStateNormal];
    chatBtu.titleTopOrBottonMarginToSuperView = -5;
    [chatBtu setBackgroundColor:RGBColor(244, 244, 244) forState:GYControlStateNormal];
    chatBtu.imageSize = CGSizeMake(20, 20);
    [chatBtu addTarget:self action:@selector(btuClick:) forControlEvents:UIControlEventTouchUpInside];
    [chatBtu setTitleColor:RGBColor(150, 150, 150) forState:GYControlStateNormal];
    chatBtu.layer.borderColor = RGBColor(220, 220, 220).CGColor;
    chatBtu.layer.borderWidth = 1.0;
    
    [self addSubview:chatBtu];
}

- (void)creatButton:(CGRect)frame title:(NSString *)title backgrounColor:(UIColor *)backgroundColor tag:(NSInteger)tag {
    
    UIButton *btu = [UIButton buttonWithFrame:frame title:title backgroundColor:backgroundColor fontSize:widthScale(16, iphone6_W) tag:tag target:self action:@selector(onClick:)];
    
    [self addSubview:btu];
}

- (void)dealloc {
    NSLog(@"bottomTool=========dealloc");
    [[NSNotificationCenter defaultCenter]removeObserver:self name:updateShopingCartGoodsNum object:nil];
}


@end
